/*
 * downloadmanager.h
 *
 *  Created on: 2010-3-23
 *      Author: alex
 */

#ifndef DOWNLOADMANAGER_H
#define DOWNLOADMANAGER_H

#include "system_define.h"
#include "addon_features.h"
#include "addon_std.h"
#include "addon_objbase.h"
#include "addon_command.h"

//#if defined(USE_S60_SYSTEM_NETWORKING) 
//#include "QStoreFile.h"
//#endif

//#include "QStoreFile.h"

//typedef enum
//{
//	
//}download_manager_error;
class CQStoreFile;
class CQDataBase;
typedef enum
{
	LIST_TYPE_WORK,
	LIST_TYPE_DONE
}task_type_e;

typedef enum
{
	qqr_shl_status_idle,
	qqr_shl_status_disconnect,
	qqr_shl_status_connecting,
	qqr_shl_status_complete
}task_shl_status_e;

typedef struct
{
	unsigned long		ref_cnt;
	unsigned short*		root_path;	
	addon_llist_t		work_list_data;
	addon_llist_t		done_list_data;
	ADDON_REF(IObject)	observer;
	unsigned short*		database_path;
	unsigned short*		postfix;
#if defined(USE_S60_SYSTEM_NETWORKING) 
	CQDataBase*         pDataBase;
#endif
	//CQStoreFile*        storeFile;
}download_manager_t,*download_manager_ptr_t;

#define 	MAX_URL			512

typedef struct
{
	addon_llink_t		link;
	unsigned short		url[MAX_URL];
	unsigned short*		filename;
	unsigned long		task_id;
	unsigned long		chapter_id;
	unsigned long		total_bytes;
	unsigned long		recvd_bytes;
	unsigned int		status;
	unsigned short*		book_id;
	ADDON_REF(IObject)	task;
}download_item_t,*download_item_ptr_t;

ADDON_INTERFACE_( IDownloadStatusObserver, 1, IObject )
{
	__INHERIT_FROM( IObject )

	ADDON_METHOD( NotifyGetLinkEvent)  ( ADDON_REF(IObject) thiz,  IN int status,IN unsigned short* filename );
	ADDON_METHOD( NotifyEvent)			( ADDON_REF(IObject) thiz,  IN int status,IN unsigned short* filename );
	ADDON_METHOD( NotifyStatus)			( ADDON_REF(IObject) thiz,  IN int status,IN unsigned short* filename );
	ADDON_METHOD( NotifyProgress)		(ADDON_REF(IObject) thiz, IN int aReceived,IN unsigned short* filename);
	ADDON_METHOD( NotifyTotalProgress)	(ADDON_REF(IObject) thiz, IN int aReceived, IN int aTotalSize,IN unsigned short* filename);
};

#define IAddonDownloadStatusObserver_VTBL( p )		              		ADDON_GET_VTBL_(p, IDownloadStatusObserver)
#define IAddonDownloadStatusObserver_CALL( p, a )		            	IAddonDownloadStatusObserver_VTBL( p )->a
#define IAddonDownloadStatusObserver_NotifyGetLinkEvent( p, a ,b)       IAddonDownloadStatusObserver_CALL( p, NotifyGetLinkEvent )( p, a ,b );
#define IAddonDownloadStatusObserver_NotifyEvent( p, a ,b)			    IAddonDownloadStatusObserver_CALL( p, NotifyEvent )( p, a ,b);
#define IAddonDownloadStatusObserver_NotifyStatus( p, a, b)           	IAddonDownloadStatusObserver_CALL( p, NotifyStatus )( p, a,b )
#define IAddonDownloadStatusObserver_NotifyProgress( p, a,b)         	IAddonDownloadStatusObserver_CALL( p, NotifyProgress )( p, a,b)
#define IAddonDownloadStatusObserver_NotifyTotalProgress( p, a ,b,c)  	IAddonDownloadStatusObserver_CALL( p, NotifyTotalProgress )( p, a ,b,c)

ADDON_INTERFACE_( IBookDownloadManager, 1, IDownloadStatusObserver )
{
	__INHERIT_FROM( IDownloadStatusObserver )
	ADDON_METHOD( AddTask)		       ( ADDON_REF(IObject) thiz, IN void* data ,IN unsigned short * path,IN unsigned short * postfix);
	ADDON_METHOD( AddOldTask)          ( ADDON_REF(IObject) thiz, IN void* data ,IN unsigned short * postfix);
	ADDON_METHOD( DealTaskOne)		   ( ADDON_REF(IObject) thiz, IN unsigned short* filename ,IN bool deletefile);
	ADDON_METHOD( SetObserver )		   ( ADDON_REF(IObject) thiz, IN ADDON_REF(IObject) obsvr );
	ADDON_METHOD( Operate)             ( ADDON_REF(IObject) thiz, IN unsigned short* filename);
//	ADDON_METHOD( Delete)              ( ADDON_REF(IObject) thiz, IN unsigned short* filename);
	ADDON_METHOD( DeleteAll)           ( ADDON_REF(IObject) thiz);
	ADDON_METHOD( StartAll)            ( ADDON_REF(IObject) thiz);
	ADDON_METHOD( PauseAll)            ( ADDON_REF(IObject) thiz);
	ADDON_METHOD( LaunchTask)          ( ADDON_REF(IObject) thiz, IN bool islaunchtask);
	ADDON_METHOD( Save)                ( ADDON_REF(IObject) thiz);
};

/*************************************************************************
* DECLARATION
*************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif	// #if defined(__cplusplus)

ADDON_IMPORT ADDON_REF(IObject)	addon_create_download_manager( void * reserved );

#if defined(__cplusplus)
}
#endif	// #if defined(__cplusplus)

/*************************************************************************
* MACRO
*************************************************************************/


#define IBookDownloadManager_VTBL( p )				    ADDON_GET_VTBL_(p, IBookDownloadManager)
#define IBookDownloadManager_CALL( p, a )				IBookDownloadManager_VTBL(p)->a
#define IBookDownloadManager_AddTask( p, a ,b ,c)		IBookDownloadManager_CALL( p, AddTask)( p, a ,b ,c)
#define IBookDownloadManager_AddOldTask( p, a ,b)		IBookDownloadManager_CALL( p, AddOldTask)( p, a ,b)
#define IBookDownloadManager_DelTaskOne( p, a ,b)		IBookDownloadManager_CALL( p, DealTaskOne )( p, a ,b)
#define IBookDownloadManager_SetObserver( p, a )		IBookDownloadManager_CALL( p, SetObserver )( p, a )
#define IBookDownloadManager_Operate( p, a )		    IBookDownloadManager_CALL( p, Operate )( p, a )
//#define IBookDownloadManager_Delete( p, a )   	        IBookDownloadManager_CALL( p, Delete )( p, a )
#define IBookDownloadManager_DeleteAll( p )		        IBookDownloadManager_CALL( p, DeleteAll )( p )
#define IBookDownloadManager_StartAll( p )		        IBookDownloadManager_CALL( p, StartAll )( p )
#define IBookDownloadManager_PauseAll( p )		        IBookDownloadManager_CALL( p, PauseAll )( p )
#define IBookDownloadManager_LaunchTask( p, a )		    IBookDownloadManager_CALL( p, LaunchTask )( p, a )
#define IBookDownloadManager_Save( p )		            IBookDownloadManager_CALL( p, Save )( p )
#endif
